from api.login import Login
from api.utils import Utils
import allure
class Medicine(Login):

    environment_config = Utils.yaml_load('config/environment.yaml')
    steps = Utils.yaml_load('api/login_steps.yaml')

    def __init__(self):
        self.req,self.resp = self.login_session('byhy','88888888')
        self.environment = self.environment_config['test_environment']['address']
        self.login_cookie = self.resp.cookies
        
    # 第一种，最简单的
    def list_medicine(self,pagesize,pagenum):

        params = {
            'action':'list_medicine',
            'pagesize':pagesize,
            'pagenum':pagenum
        }
        rs = self.req.get('http://127.0.0.1:80/api/mgr/medicines',params=params)
        return rs

    # 第二种，把api拆成yaml,
    # 这里要用到session来发送请求，就很麻烦
    def list_medicine_api(self,pagesize,pagenum):
        steps = self.steps['list_medicine']
        step_rep = Utils.string_replace(steps,pagesize=pagesize,pagenum=pagenum)
        step_rep.update({'cookies':self.login_cookie})
        return self.send_api(step_rep)

    @allure.step('添加药品')
    def add_medicine(self,name,desc,sn):

        api_utl = self.get_address(self.environment,'api/mgr/medicines')
        print(api_utl)
        data = {
            'action':'add_medicine',
            'data':{
                'name':name,
                'desc':desc,
                'sn':sn
            }
        }
        
        rs = self.req.post(api_utl,json=data,proxies={
            'http': 'http://127.0.0.1:8888',
            'https': 'http://127.0.0.1:8888'
        })
        return rs
    
    def delete_medicine(self,id):

        api_utl = self.get_address(self.environment,'api/mgr/medicines')

        data = {
            'action': 'del_medicine',
            'id':id
        }
        rs = self.req.delete(api_utl,json=data,proxies={
            'http': 'http://127.0.0.1:8888',
            'https': 'http://127.0.0.1:8888'
        })
        return rs